package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JRadioButton;

import application.PersistanceManager;

import model.ModelFacade;
import model.Restaurant;

import view.SommaireUi;
import view.VueAdresse;
import view.VueApplication;
import view.VueConfirmation;
import view.VuePlats;
import view.VueRestaurant;

public class PlacerCommandeController {

	
	public PlacerCommandeController(){
		mf = ModelFacade.getInstance();
		vApplication = new VueApplication();
		vApplication.addQuitListener(new QuitListener());
		vApplication.addPlacerCommandeListener(new PlacerCommandeListener());
		mf.attachObserverToCarnetCommandes(new PersistanceManager());
	}

	private class PlacerCommandeListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			JMenu menu = vApplication.getMenu();
			if(e.getSource() == menu.getItem(0)){
				vRestaurants = new VueRestaurant();
				vRestaurants.addRestaurantListener(new RestaurantListener());
				vApplication.setVisible(false);
			}
		}
	}


	/* Quit handler */
	private class QuitListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			System.exit(0);
		}
	}
	
	private class RestaurantListener implements ActionListener {
		public void actionPerformed(ActionEvent e){
			String selectedRestaurant = ((JRadioButton)e.getSource()).getText();
			Restaurant resto = null;
			List<Restaurant> liste = mf.getListeRestaurants();
			for(int i = 0; i < liste.size(); i++){
				if(liste.get(i).getNom().equals(selectedRestaurant)){
					resto = liste.get(i);
					break;
				}
			}
			mf.setCurrentRestaurant(resto);
			vPlats = new VuePlats(resto);
			vPlats.addPlatSelectionListener(new PlatSelectionListener());
			vRestaurants.setVisible(false);
		}
	}
	
	private class PlatSelectionListener implements ActionListener{

		public void actionPerformed(ActionEvent e) {
			mf.setCurrentSelection(vPlats.getSelection());
			vSommaire = new SommaireUi();
			vSommaire.addBtnOkListener(new BtnOkListener());
			vPlats.setVisible(false);
		}
		
	}
	
	private class BtnOkListener implements ActionListener{
		
		public void actionPerformed(ActionEvent e)
		{
	    	if (((JButton)e.getSource()).getText().equalsIgnoreCase("Ok")){
			vAdresse = new VueAdresse();
			vAdresse.addAdresseListener(new AdresseListener());
			vSommaire.setVisible(false);
		 	}
		}
	}
	
	private class AdresseListener implements ActionListener{

		public void actionPerformed(ActionEvent e) {
			//if(((JButton)e.getSource()).getText().equalsIgnoreCase("Confirmer")){
				if(vAdresse.isValidInfo()){
					vConfirmation = new VueConfirmation();
					vConfirmation.addConfirmationOkListener(new ConfirmationOkListener());
					vAdresse.setVisible(false);
					mf.attachObserverToCarnetCommandes(vConfirmation);
					mf.setCurrentAdress(vAdresse.getAdresseLivraison());
					mf.setCurrentDeliveryDate(vAdresse.getDateLivraison());
					mf.setCurrentDeliveryTime(vAdresse.getHeureLivraison());
					mf.placeCommande();
					
				}
			//}
			
		}
		
	}
	
	private class ConfirmationOkListener implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			System.exit(0);
		}
	}

	
	private VueApplication vApplication;
	private VueAdresse vAdresse;
	private SommaireUi vSommaire;
	private VueRestaurant vRestaurants;
	private VuePlats vPlats;
	private VueConfirmation vConfirmation;
	private ModelFacade mf;
	
}
